using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Remove, "PsSpecCmdlet", SupportsShouldProcess = true, DefaultParameterSetName = "ById")]
    public class RemovePsSpecCmdletCommand : Cmdlet
    {
        // ById Parameter Set
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "ById")]
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                idSpecified = true;
            }
        }
        private int _id = -1;
        private bool idSpecified = false;

        // ByName Parameter Set
        [Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        public String ProjectName
        {
            get
            {
                return project;
            }
            set
            {
                project = value;
            }
        }
        private string project;

        [Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName="ByName")]
        [ValidateLength(1, 50)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = "*";

        [Parameter]
        public SwitchParameter Force
        {
            get
            {
                return _force;
            }
            set
            {
                _force = value;
            }
        }
        private SwitchParameter _force;

        [Parameter(Mandatory = false)]
        public String ServiceUri
        {
            get
            {
                return _serviceUri;
            }
            set
            {
                _serviceUri = value;
            }
        }
        private string _serviceUri = PsSpecCmdletUtils.Constants.ServiceUri;


        protected override void ProcessRecord()
        {
            // Remove multiple cmdlets if specified by name
            if (! idSpecified)
            {
                foreach (PsSpecCmdlet cmdlet in GetCmdlets(ProjectName, Name))
                {
                    RemoveCmdlet(cmdlet.Id);
                }
            }
            else
            {
                RemoveCmdlet(Id);
            }
        }

        private IEnumerable<PsSpecCmdlet> GetCmdlets(string project, string name)
        {
            GetPsSpecCmdletCommand command = new GetPsSpecCmdletCommand();
            command.ServiceUri = ServiceUri;
            command.ProjectName = project;
            command.Name = name;

            foreach (Object result in command.Invoke())
            {
                yield return (PsSpecCmdlet)result;
            }
        }

        private void RemoveCmdlet(int id)
        {
            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.RemoveCmdletShouldProcess, id)))
            {
                PsSpecCmdletService.CmdletService service =
                    new PsSpecCmdletService.CmdletService();
                service.Url = ServiceUri;
                service.UseDefaultCredentials = true;

                if (_force || ShouldContinue(
                    String.Format(resources.ReallyWantToRemoveCmdletDescription, id),
                    resources.ReallyWantToRemoveCmdletTitle))
                {
                    try
                    {
                        service.RemoveCmdlet(id);
                    }
                    catch (System.Web.Services.Protocols.SoapException e)
                    {
                        WriteError(new ErrorRecord(e, "SERVICE_ERROR", ErrorCategory.InvalidOperation, id));
                    }
                }
            }
        }
    }
}
